/* Copyright (c) 2012 William Steven Knauer
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.williamknauer.android.yagol;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.quietlycoding.android.picker.NumberPicker;

/**
 * Allows the selection of simulation dimensions.
 * 
 * @author William S Knauer <knauerw1@gmail.com>
 * @version 2012.0111
 */
public class DimensionsPickerActivity extends Activity {
	private NumberPicker mRowPicker;
	private NumberPicker mColPicker;
	private Button mCreateButton;
	private Button mCancelButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dimensions_picker);

		/*
		 * Initialize the row picker.
		 */
		mRowPicker = (NumberPicker) findViewById(R.id.row_picker);
		mRowPicker.setRange(1, 1000);
		mRowPicker.setSpeed(50);

		/*
		 * Initialize the column picker
		 */
		mColPicker = (NumberPicker) findViewById(R.id.col_picker);
		mColPicker.setRange(1, 1000);
		mColPicker.setSpeed(50);

		/*
		 * Set up listener for cancel button.
		 */
		mCancelButton = (Button) findViewById(R.id.cancel_button);
		mCancelButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish(RESULT_CANCELED);
			}
		});

		/*
		 * Set up listener for create button.
		 */
		mCreateButton = (Button) findViewById(R.id.create_button);
		mCreateButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish(RESULT_OK);
			}
		});

		/*
		 * Restore any existing previous values.
		 */
		if (savedInstanceState != null) {
			mRowPicker.setCurrent(savedInstanceState.getInt("rows"));
			mColPicker.setCurrent(savedInstanceState.getInt("cols"));
		} else {
			mRowPicker.setCurrent(200);
			mColPicker.setCurrent(200);
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		/*
		 * Save the current values of the number pickers.
		 */
		syncNumberPickerValue();
		outState.putInt("rows", mRowPicker.getCurrent());
		outState.putInt("cols", mColPicker.getCurrent());
	}

	private void syncNumberPickerValue() {
		/*
		 * TODO REMOVE THIS HORRIBLE HACK!
		 */
		if (mRowPicker.hasFocus()) {
			mColPicker.requestFocus();
		}
		if (mColPicker.hasFocus()) {
			mRowPicker.requestFocus();
		}
	}

	public void finish(int resultCode) {
		/*
		 * Set row and column data in result.
		 */
		Intent i = new Intent();
		syncNumberPickerValue();
		i.putExtra("com.williamknauer.android.yagol.DimensionsPickerActivity.rows",
				mRowPicker.getCurrent());
		i.putExtra("com.williamknauer.android.yagol.DimensionsPickerActivity.cols",
				mColPicker.getCurrent());
		setResult(resultCode, i);

		/*
		 * Close this activity.
		 */
		super.finish();
	}

}
